﻿var PageViewModelBase = new function () {

    this.Name = 'PageViewModelBase';

    this.Item = new function () {

        this.Title = '';
        this.Description = '';
        this.Url = '';
        this.Date = new Date();

        this.New = function () {
            this.Date = new Date();
            return ko.mapping.fromJS($.extend({}, this));
        };
    };

    this.Items = ko.observableArray([]);

    this.ItemFields = function (addBlankAtFirst) {
        var fields = [];
        addBlankAtFirst = addBlankAtFirst == null ? true : false;
        if (addBlankAtFirst)
            fields.push('');
        for (var i in this.Item) {
            if (typeof (this.Item[i]) == 'function')
                continue;
            fields.push(i);
        }
        return fields;
    };
    this.CurrentItemField = ko.observable('');

    this.MapFromJS = function (data) {

        if (!data || !data.Items)
            return;
        //throw new Error("The data source is not properly formatted, should be an extension of: {Items:[]}");

        for (var i in data.Items) {
            data.Items[i] = $.extend(ko.mapping.toJS(this.Item.New()), data.Items[i]);
        };

        ko.mapping.fromJS(data, { 'ignore': ['Name', 'Item'] }, this);
    };

    this.MapFromJSON = function (dataString) {
        this.MapFromJS($.parseJSON(dataString));
    };

    this.ToJS = function () {
        return ko.mapping.toJS(this);
    };

    this.ToJSON = function () {
        return ko.mapping.toJSON(this);
    };

    this.Save = function (callback) {
        ModalOverlay.Show(AppSettings.ProcessingIndicator);
        $.ajax(AppSettings.FileSaveUrl, {
            data: { FileName: this.Name + ".json", Content: this.ToJSON() },
            dataType: 'json',
            type: 'POST',
            beforeSend: function (jqXHR, settings) {
                ModalOverlay.Show(AppSettings.ProcessingIndicator);
            },
            complete: function (jqXHR, textStatus) {
                ModalOverlay.Hide();
            },
            success: function (data, textStatus, jqXHR) {
                NotifierShow(data.UserFriendlyMessage, data.DetailsMessage);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                NotifierShow("An error occured while trying to save the data", errorThrown);
            }
        });
    };

    this.Load = function (callback) {
        var self = this;
        $.ajax(AppSettings.FileLoadUrl, {
            data: { FileName: this.Name + ".json" },
            dataType: 'json',
            type: 'POST',
            beforeSend: function (jqXHR, settings) {
                ModalOverlay.Show(AppSettings.LoadingIndicator);
            },
            complete: function (jqXHR, textStatus) {
                ModalOverlay.Hide();
            },
            success: function (data, textStatus, jqXHR) {
                self.MapFromJSON(data.Content);
                if (callback && typeof (callback) == "function")
                    callback();
            },
            error: function (jqXHR, textStatus, errorThrown) {

            }
        });
    };
};